﻿<UserControl x:Class="CRM.Tools.ODataQueryDesigner.QueryDesigner" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    xmlns:local="clr-namespace:CRM.Tools.ODataQueryDesigner"
    xmlns:mefed="http:\\www.codeplex.com\MEFedMVVM"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=System.Windows"
    mefed:ViewModelLocator.ViewModel="QueryDesignerViewModel"
    mc:Ignorable="d"
    d:DesignHeight="1000" d:DesignWidth="1000">

    <UserControl.Resources>
        <local:ObjectConverter x:Key="ConvertObject" />
        <local:EnumBoolConverter x:Key="ConvertEnum" />
        <local:AttributeTextValueConverter x:Key="ConvertTextValue" />
    </UserControl.Resources>
    
    <Grid x:Name="LayoutRoot">
       
        <sdk:TabControl  Margin="5,5,0,5" >
                <sdk:TabItem Header="Create Query" >
                    
                    <ScrollViewer Style="{StaticResource PageScrollViewerStyle}">
                   
                    <StackPanel >
                        <TextBox x:Name="uxODataQuery"  TextWrapping="Wrap" Height="60" AcceptsReturn="True" VerticalScrollBarVisibility="Visible"
                                      Text="{Binding Path=ODataQuery, Mode=TwoWay}" />
                        <Grid >
                                <Grid.ColumnDefinitions >
                                    <ColumnDefinition Width="150" />
                                    <ColumnDefinition Width="150" />
                                    <ColumnDefinition Width="200" />
                                    <ColumnDefinition Width="200" />
                                <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Button Content="Generate" Grid.Column="0" HorizontalAlignment="Left"  VerticalAlignment="Top" Margin="0,0,0,0" Width="100" Height="23" IsEnabled="{Binding Path=IsAllowedToGenerate}" Command="{Binding Path=Generate_Click}" />
                                <Button Content="Execute"  Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,0,0" Width="100" Height="23"  IsEnabled="{Binding Path=IsAllowedToExecuteREST}" Command="{Binding Path=Execute_Click}" />
                                <TextBlock Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Center" Width="Auto" Margin="5,0,5,0" FontSize="12" FontWeight="Bold"  Text="{Binding Path=ResultXml}" />
                            <Button Content="Show In Browser"  Grid.Column="3" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,0,0,0" Width="100" Height="23"  IsEnabled="{Binding Path=IsAllowedToOpenBrowser}" Command="{Binding Path=OpenBrowserWithQuery_Click}" />
                                <TextBlock Grid.Column="4" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="Auto" Margin="0,0,5,0" FontSize="12" FontWeight="Bold"  Text="{Binding Path=QueryLength}" />
                            </Grid>
                            <Grid>
                                <Grid.ColumnDefinitions >
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>

                                <Grid Grid.Column="0" HorizontalAlignment="Stretch" MinHeight="40" >
                                    <Grid.ColumnDefinitions >
                                        <ColumnDefinition Width="120" />
                                        <ColumnDefinition  />
                                    </Grid.ColumnDefinitions>

                                    <TextBlock Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Bold" Text="Select Entity Set" />
                                    <ComboBox Grid.Column="1" HorizontalAlignment="Stretch"  VerticalAlignment="Center" ItemsSource="{Binding Path=EntityList,  Mode=OneWay}" DisplayMemberPath="SchemaNameSet" SelectedValuePath="LogicalName" SelectedItem="{Binding Path=SelectedEntity, Mode=TwoWay}" />
                                </Grid>
                            </Grid>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="40" />
                                    <RowDefinition Height="20" />
                                    <RowDefinition Height="200" />
                                </Grid.RowDefinitions>
                                <TextBlock Grid.Row="0" Text="Filter Criteria" FontWeight="Bold" VerticalAlignment="Center" />
                                <StackPanel Grid.Row="1" Orientation="Horizontal">
                                    <Button Content="Group AND" Margin="5,0,5,0"  Command="{Binding Path=AddGroupAND_Click}" />
                                    <Button Content="Group OR" Margin="5,0,5,0" Command="{Binding Path=AddGroupOR_Click}" />
                                </StackPanel>
                                <ItemsControl Grid.Row="2" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"   ItemsSource="{Binding Path=FiltersList}"  >
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate >
                                            <Grid  HorizontalAlignment="Stretch" Height="30" Background="{Binding Path=RowColour}"  >
                                                <!-- The attribute picklist is shown first -->
                                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Center"  Visibility="{Binding Path=AttributeListVisibilityLevel}" >
                                                <ComboBox Width="450" Margin="5,0,1,0" MaxDropDownHeight="400" ItemsSource="{Binding Path=Attributes}" DisplayMemberPath="UserLocalizedLabel" SelectedItem="{Binding Path=SelectedAttribute, Mode=TwoWay}" />
                                                </StackPanel>
                                                <!-- Once the attribute is selected then this full row of options is shown -->
                                                <StackPanel  Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Center" Visibility="{Binding Path=SelectFilterVisibilityLevel}" >
                                                    <Grid VerticalAlignment="Center">
                                                        <ComboBox Width="35" Margin="5,0,1,0" ItemsSource="{Binding Path=FilterActions}" SelectedItem="{Binding Path=FilterAction, Mode=TwoWay}" Visibility="{Binding Path=FilterActionsVisibilityLevel}"   />
                                                        <TextBlock Width="35" Margin="5,0,1,0" Text="{ Binding Path=GroupLogicalOperator }" Visibility="{Binding Path=LogicalOperatorVisibilityLevel}" />
                                                    </Grid>
                                                <ComboBox Width="350" Margin="1,0,1,0" MaxDropDownHeight="400" ItemsSource="{Binding Path=Attributes}" DisplayMemberPath="UserLocalizedLabel" SelectedItem="{Binding Path=SelectedAttribute, Mode=TwoWay}" />
                                                    <ComboBox Width="200" Margin="1,0,1,0" ItemsSource="{Binding Path=FilterOperators}" SelectedItem="{Binding Path=FilterOperator, Mode=TwoWay}"  />

                                                    <Grid VerticalAlignment="Center">
                                                        <TextBlock Width="300" />
                                                        <TextBox Width="300" Margin="1,0,1,0" Text="{Binding Path=Value, Mode=TwoWay}" Visibility="{Binding Path=TextValueVisibilityLevel}" />
                                                        <ComboBox Width="300" Margin="1,0,1,0" ItemsSource="{Binding Path=AttributeValues}" DisplayMemberPath="Name" SelectedValuePath="Value" SelectedItem="{Binding Path=Value, Mode=TwoWay, Converter={StaticResource ConvertTextValue}}"  Visibility="{Binding Path=OptionSetValueVisibilityLevel}" />
                                                        
                                                    </Grid>
                                                    <ComboBox Width="60" Margin="1,0,1,0" ItemsSource="{Binding Path=LogicalOperators}" SelectedItem="{Binding Path=LogicalOperator, Mode=TwoWay}" />
                                                </StackPanel>
                                            </Grid>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>

                            </Grid>
                            <Grid>

                                <Grid.ColumnDefinitions >
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>

                                <StackPanel  Grid.Column="0">


                                    <sdk:TabControl>
                                        <sdk:TabItem Header="Entity Attributes" >
                                            <Grid>

                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="40" />
                                                    <RowDefinition Height="*" />
                                                </Grid.RowDefinitions>

                                                <ListBox Grid.Row="1" Height="250" HorizontalAlignment="Stretch" ItemsSource="{Binding AttributeList}">
                                                    <ListBox.ItemTemplate>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal" Width="180" VerticalAlignment="Top" HorizontalAlignment="Left">
                                                                <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Width="16"  Margin="2,2,0,0" ClickMode="Press"></CheckBox>
                                                                <TextBlock Text="{Binding Path=UserLocalizedLabel}" FontSize="12" FontFamily="Arial" Margin="5,0,0,0"></TextBlock>
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </ListBox.ItemTemplate>
                                                </ListBox>
                                            </Grid>
                                        </sdk:TabItem>
                                        <sdk:TabItem Header="One To Many" >
                                            <Grid>

                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="40" />
                                                    <RowDefinition Height="*" />
                                                </Grid.RowDefinitions>


                                                <ComboBox Name="uxOneToMany" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="2"   ItemsSource="{Binding Path=OneToMany_RelationshipList,  Mode=OneWay}"  DisplayMemberPath="SchemaName"  SelectedItem="{Binding Path=SelectedOneToManyRelationship, Mode=TwoWay}" />

                                                <ListBox  Grid.Row="1" Height="250" HorizontalAlignment="Stretch" ItemsSource="{Binding OneToMany_RelationshipAttributeList}"    >
                                                    <ListBox.ItemTemplate>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal" Width="180" VerticalAlignment="Top" HorizontalAlignment="Left">
                                                                <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Width="14" Height="14" Margin="2,2,0,0" ClickMode="Press"></CheckBox>
                                                                <TextBlock Text="{Binding Path=UserLocalizedLabel}" FontSize="12" FontFamily="Arial" Margin="5,0,0,0"></TextBlock>
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </ListBox.ItemTemplate>
                                                </ListBox>

                                            </Grid>

                                        </sdk:TabItem>

                                        <sdk:TabItem Header="Many To One" >
                                            <Grid>

                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="40" />
                                                    <RowDefinition Height="*" />
                                                </Grid.RowDefinitions>
                                                <ComboBox Name="uxManyToOne" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="2"   ItemsSource="{Binding Path=ManyToOne_RelationshipList,  Mode=OneWay}"  DisplayMemberPath="SchemaName"  SelectedItem="{Binding Path=SelectedManyToOneRelationship, Mode=TwoWay}" />

                                                <ListBox Grid.Row="1" Height="250" HorizontalAlignment="Stretch" ItemsSource="{Binding Path=ManyToOne_RelationshipAttributeList}"    >
                                                    <ListBox.ItemTemplate>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal" Width="180" VerticalAlignment="Top" HorizontalAlignment="Left">
                                                            <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Width="14" Height="14" Margin="2,2,0,0" ClickMode="Press"></CheckBox>
                                                            <TextBlock Text="{Binding Path=UserLocalizedLabel}" FontSize="12" FontFamily="Arial" Margin="5,0,0,0"></TextBlock>
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </ListBox.ItemTemplate>
                                                </ListBox>
                                            </Grid>

                                        </sdk:TabItem>

                                        <sdk:TabItem Header="Many To Many" >
                                            <Grid>

                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="40" />
                                                    <RowDefinition Height="*" />
                                                </Grid.RowDefinitions>
                                                <ComboBox Name="uxManyToMany" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="2"   ItemsSource="{Binding Path=ManyToMany_RelationshipList,  Mode=OneWay}"  DisplayMemberPath="SchemaName"  SelectedItem="{Binding Path=SelectedManyToManyRelationship, Mode=TwoWay}" />

                                                <ListBox  Grid.Row="1" Height="250" HorizontalAlignment="Stretch" ItemsSource="{Binding Path=ManyToMany_RelationshipAttributeList}"    >
                                                    <ListBox.ItemTemplate>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal" Width="180" VerticalAlignment="Top" HorizontalAlignment="Left">
                                                            <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Width="14" Height="14" Margin="2,2,0,0" ClickMode="Press"></CheckBox>
                                                                <TextBlock Text="{Binding Path=UserLocalizedLabel}" FontSize="12" FontFamily="Arial" Margin="5,0,0,0"></TextBlock>
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </ListBox.ItemTemplate>
                                                </ListBox>
                                            </Grid>

                                        </sdk:TabItem>
                                    </sdk:TabControl>

                                </StackPanel>
                                <StackPanel  Grid.Column="2">
                                    <TextBlock Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Bold" Height="20" Text="Order By Criteria" />

                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Grid.Column="0" x:Name="Sort1Label" Text="Sort By" Margin="5,2,0,0" VerticalAlignment="Top" TextWrapping="Wrap" TextAlignment="Left"/>
                                        <StackPanel Grid.Column="1">
                                        <ComboBox HorizontalAlignment="Stretch" ItemsSource="{Binding Path=SortByAttributesList}" DisplayMemberPath="UserLocalizedLabel" SelectedItem="{Binding Path=SelectedSortBy1Attribute, Mode=TwoWay}" />
                                            <RadioButton  GroupName="Sort1Direction" Content="Ascending Order" IsChecked="{Binding Path=SortBy1Direction, Mode=TwoWay, Converter={StaticResource ConvertEnum}, ConverterParameter=asc}" />
                                            <RadioButton  GroupName="Sort1Direction" Content="Descending Order" IsChecked="{Binding Path=SortBy1Direction, Mode=TwoWay, Converter={StaticResource ConvertEnum}, ConverterParameter=desc}" />

                                        </StackPanel>
                                    </Grid>
                                    <Grid Margin="0,10,0,0">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Grid.Column="0" x:Name="Sort2Label" Text="Then By" Margin="5,2,0,0" VerticalAlignment="Top" TextWrapping="Wrap" TextAlignment="Left"/>
                                        <StackPanel Grid.Column="1">
                                        <ComboBox HorizontalAlignment="Stretch" ItemsSource="{Binding Path=SortByAttributesList}" DisplayMemberPath="UserLocalizedLabel" SelectedItem="{Binding Path=SelectedSortBy2Attribute, Mode=TwoWay}" />
                                            <RadioButton  GroupName="Sort2Direction" Content="Ascending Order" IsChecked="{Binding Path=SortBy2Direction, Mode=TwoWay, Converter={StaticResource ConvertEnum}, ConverterParameter=asc}" />
                                            <RadioButton  GroupName="Sort2Direction" Content="Descending Order" IsChecked="{Binding Path=SortBy2Direction, Mode=TwoWay, Converter={StaticResource ConvertEnum}, ConverterParameter=desc}" />

                                        </StackPanel>
                                    </Grid>

                                </StackPanel >

                            </Grid>


                        </StackPanel >
                    </ScrollViewer>
                </sdk:TabItem>
                <!-- Create Query -->


                <sdk:TabItem Header="Results (ATOM)" >
                    <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="5,5,5,5" Text="{Binding Path=ATOMResultXml}"  
                        HorizontalScrollBarVisibility="Visible"
                        VerticalScrollBarVisibility="Visible"
                        TextWrapping="NoWrap" />
                </sdk:TabItem>
            <sdk:TabItem Header="Results (JSON)" >
                <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="5,5,5,5" Text="{Binding Path=JSONResultXml}"  
                        HorizontalScrollBarVisibility="Visible"
                        VerticalScrollBarVisibility="Visible"
                        TextWrapping="NoWrap" />
            </sdk:TabItem>
            


        </sdk:TabControl>



        

    </Grid>

</UserControl>